package com.eboss2.sbs.activity;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.AdapterGoodsAttrList;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.GoodAttrsQb;
import com.eboss2.sbs.model.GoodsVOQb;
import com.eboss2.sbs.myapplication.MyApplication;
import com.eboss2.sbs.util.HtmlFormat;
import com.eboss2.sbs.weight.ADCommonView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.ScreenUtils;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.view.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品详情
 * 
 * @author Ivan
 * @Description 存在2中商品
 */
public class ActivityProductDetails extends BaseActivity implements OnClickListener {
	private final static String TAG = "ActivityProductDetails";
	private long goodsId;
	private GoodsVOQb voqb;
	private LinearLayout ll_advertis;

	private MyGridView gridView;
	private AdapterGoodsAttrList adapterGoodsAttrList;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_product_details);
		MyActivityManager.getInstance().addActivity(this);

		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "商品详情 ");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);


		initView();
		loadData();

	}

	@Override
	protected void onResume() {
		super.onResume();
	}


	private void initView() {
		// 广告栏开始
		ll_advertis = (LinearLayout) findViewById(R.id.advertis);
		// 设置宽度高度一致
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) ll_advertis.getLayoutParams();
		linearParams.height = ScreenUtils.getScreenWidth(mContext)*4/5;
		ll_advertis.setLayoutParams(linearParams);
		// 广告栏结束


		goodsId = getIntent().getLongExtra("id", 0);


	}


	private void loadData() {
		showProgress("正在加载...");
		// String url = ConfigApp.HC_GET_STORE_GOODS_DETAILS;
		String url = Config.SBS_GOODS_DETAIL;
		RequestParams params = new RequestParams();
		params.put("id", goodsId);
		params.put("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId());
		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@Override
			public void success(String response) {
				disShowProgress();


				if (!StringUtil.isBlank(response)) {

					Gson gson = new Gson();
					// json数据转换成List
					voqb = gson.fromJson(response, new TypeToken<GoodsVOQb>() {
					}.getType());

					if (voqb != null) {

						setData();
					}

				}

			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showErrorMsg("网络连接失败，请稍候再试！");
			}
		});
	}


	private void setData(){
		List<String> imgPaths = new ArrayList<String>();
		if (voqb.getPicture() != null) {
			String[] images = StringUtils.spiltStrings(voqb.getPicture(), ",");
			// 轮播图片

			for (int i = 0; i < images.length; i++) {
				imgPaths.add(images[i]);
			}
		}


		ADCommonView adView = new ADCommonView(mContext, imgPaths);
		ll_advertis.addView(adView);

		setDataQb();
	}




	@SuppressLint("WrongConstant")
	protected void setDataQb() {
		textView(R.id.tv_goods_name).setText(StringUtils.isBlank(voqb.getName()) ? "" : voqb.getName());

		textView(R.id.tv_vip_price).setText("会员价: ￥" + StringUtils.formatIntMoney(voqb.getPromotionPrice()));
		textView(R.id.tv_no_vip_price).setText("现价: ￥" + StringUtils.formatIntMoney(voqb.getPromotionPrice2()));
		textView(R.id.tv_price).setText("原价： ￥" + StringUtils.formatIntMoney(voqb.getSalePrice()));
		textView(R.id.tv_price).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // 中间横线
		textView(R.id.tv_cb_price).setText("供货价： ￥" + StringUtils.formatIntMoney(voqb.getSupplyPrice()));
		textView(R.id.id_sale_volume).setText("已有" + voqb.getSaleNum() + "人购买");
		textView(R.id.id_onhand).setText("库存数: " + voqb.getStockNumber()+"");
		StringBuilder builder = new StringBuilder();
		builder.append(StringUtils.isBlank(voqb.getCategoryFirstName()) ? "" : voqb.getCategoryFirstName());
		builder.append(StringUtils.isBlank(voqb.getCategorySecondName()) ? "" : ">"+voqb.getCategorySecondName());
		textView(R.id.id_category_name).setText("类目:"+builder.toString());
		textView(R.id.tv_ems_content).setText("邮寄方式:"+(StringUtils.isBlank(voqb.getPostMethodStr()) ? "" : voqb.getPostMethodStr()));
		textView(R.id.id_weight_num).setText("重量:"+voqb.getWeight()+"g");
		if (voqb.getLimitNumber() != null && voqb.getLimitNumber() > 0) {
			textView(R.id.tv_sale_num).setText("每人限购:" + voqb.getLimitNumber() + "件");
		}else{
			textView(R.id.tv_sale_num).setVisibility(View.GONE);
		}

		List<GoodAttrsQb> goodAttrsQbs = new ArrayList<GoodAttrsQb>();

		if (!StringUtils.isBlank(voqb.getAttribute())) {

				HashMap<String,String> maps = new Gson().fromJson(voqb.getAttribute(), new TypeToken<HashMap<String,Object>>(){}.getType());
				for (Object map : maps.entrySet()){
					String key = (String) ((Map.Entry)map).getKey();
					String value = (String)((Map.Entry)map).getValue();

					GoodAttrsQb goodAttrsQb = new GoodAttrsQb();
					goodAttrsQb.setGoodsAttrsName(key);
					goodAttrsQb.setGoodsAttrsValue(value);
					goodAttrsQbs.add(goodAttrsQb);
				}
		}

		gridView = findViewById(R.id.gridView);
		adapterGoodsAttrList = new AdapterGoodsAttrList(mContext, goodAttrsQbs);
		gridView.setAdapter(adapterGoodsAttrList);

		adapterGoodsAttrList.notifyDataSetChanged();


		// 详情介绍
		if (!StringUtils.isBlank(voqb.getContent())) {
			setDataDetail(voqb.getContent());
		}





	}

	private void setDataDetail(String data) {

		WebView mWebView = (WebView) findViewById(R.id.web_view);
		mWebView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(data),"text/html","utf-8",null);

	}




	/*
	 * (非 Javadoc) Description:
	 *
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			default:
				break;
		}

	}




}
